package com.heima.article.controller.v1;

import com.heima.article.service.ApArticleService;
import com.heima.common.constants.article.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/article")
public class ApArticleController {
    @Autowired
    ApArticleService apArticleService;

    /**
     * 保存app文章
     *
     * @param dto
     * @return
     */
    @PostMapping("/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto dto) {
        return apArticleService.saveArticle(dto);
    }

    /**
     * 根据文章id查询文章
     *
     * @param id
     * @return
     */
    @GetMapping("/one/{id}")
    public ApArticle findArticleById(@PathVariable("id") Long id) {
        return apArticleService.getById(id);
    }

    /**
     * 更新文章
     *
     * @param apArticle
     * @return
     */
    @PutMapping("/updateArticleById")
    public boolean updateArticleById(@RequestBody ApArticle apArticle) {
        return apArticleService.updateById(apArticle);
    }

    /**
     * app端首页文章展示
     *
     * @param dto
     * @return
     */
    @PostMapping("/load")
    public ResponseResult load(@RequestBody ArticleHomeDto dto) {
        return apArticleService.load2(ArticleConstants.LOADTYPE_LOAD_MORE, dto, true);
    }

    /**
     * 下滑操作:获取更多
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadmore")
    public ResponseResult loadmore(@RequestBody ArticleHomeDto dto) {
        return apArticleService.load2(ArticleConstants.LOADTYPE_LOAD_MORE, dto, false);
    }

    /**
     * 上拉操作:获取最新
     *
     * @param dto
     * @return
     */
    @PostMapping("/loadnew")
    public ResponseResult loadnew(@RequestBody ArticleHomeDto dto) {
        return apArticleService.load2(ArticleConstants.LOADTYPE_LOAD_NEW, dto, false);
    }
}
